package com.yonyou.pmclouds.alter.rmiimpl;


import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.alter.entity.AlterVO;
import com.yonyou.pmclouds.alter.mapper.AlterMapper;
import com.yonyou.pmclouds.alter.process.AlterInsertCheckProcessor;
import com.yonyou.pmclouds.alter.process.AlterProjectLogPostProcess;
import com.yonyou.pmclouds.alter.rmiitf.AlterMaintain;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.MobileTaskInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.ProjectStatusCheckProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.picture.entity.PictureParamVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.task.rmiitf.TaskRemoteService;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= AlterMaintain.class)
public class AlterMaintainImpl implements AlterMaintain,IFlowApproveBuziService{

    @Autowired
    private AlterMapper mapper;
    @Autowired
    private PictureRemoteService pictureService;
    @Autowired
    private TaskRemoteService taskService;
    @Autowired
    private MobileTaskInsertProcessor<AlterVO> mobileTaskInsertProcessor;
    @Autowired
    private ProjectStatusCheckProcessor<AlterVO> projectStatusCheckProcessor;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<AlterVO> billCountFactory;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<AlterVO> billForUpdateProcessorFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<AlterVO> billCountDeleteFactory;
    @Autowired
    private AlterProjectLogPostProcess alterProjectLogPostProcess;

    private static String[] UPDATE_STATUS = {AlterVO.BILLSTATUS};
    private static String[] UPDATE_FIELDS = {AlterVO.PK_ALTERCATEGORY, AlterVO.APPLYDATE, AlterVO.ESTIMATEMNY, AlterVO.ALTERCONTENT, AlterVO.BILLSTATUS};

    @Override
    @PMApprove
    public AlterVO insert(AlterVO alterVO) throws BusinessException {
        if(alterVO == null){
            return null;
        }
        initInsertVO(alterVO);   //补全字段信息
        SingleInsertBpTemplate<AlterVO> bp = new SingleInsertBpTemplate<>(mapper);//生成UUID
        //更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<AlterVO>());//补全时间
        bp.addBeforeProcessor(new AlterInsertCheckProcessor());//检验添加是否为空等
        bp.addBeforeProcessor(projectStatusCheckProcessor);//检验项目状态
        //更新单据数
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.ALTER_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(alterProjectLogPostProcess);
        bp.insert(alterVO);
        // 保存图片
        pictureService.insertPictureForPub(alterVO, null);

        return alterVO;
    }

    @Override
    @PMApprove(trigger = "commit")
    public AlterVO update(AlterVO alterVO) throws BusinessException {
        if(alterVO == null){
            return null;
        }
        initInsertVO(alterVO);   //补全字段信息
        SingleUpdateBpTemplate<AlterVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<AlterVO>());
        bp.addBeforeProcessor(new AlterInsertCheckProcessor());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.ALTER_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(alterProjectLogPostProcess);
        bp.update(alterVO);
        //更新单据图片
        pictureService.updateBillPicture(alterVO, new PictureParamVO(null, null, BillTypeConst.ALTER_BILLTYPE));
        return alterVO;
    }

    @Override
    public AlterVO approve(String pkAlter, byte billStatus) throws BusinessException {
        if(StringUtils.isEmpty(pkAlter)){
            return null;
        }
        AlterVO alterVO = mapper.getById(pkAlter);
        SingleUpdateBpTemplate<AlterVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);

        alterVO.setBillstatus(billStatus);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.ALTER_BILLTYPE));
        bp.update(alterVO);

        return alterVO;
    }

    @Override
    public AlterVO reject(String pkAlter) throws BusinessException {
        if(StringUtils.isEmpty(pkAlter)){
            return null;
        }
        AlterVO alterVO = mapper.getById(pkAlter);
        SingleUpdateBpTemplate<AlterVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        alterVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.ALTER_BILLTYPE));
        bp.update(alterVO);
        return alterVO;
    }

    private void initInsertVO(AlterVO vo){
        if(vo == null) return;
        //单据类型
        vo.setBill_type(BillTypeConst.ALTER_BILLTYPE);
        //单据状态
        vo.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
        //租户
        if(StringUtils.isEmpty(vo.getPkTenant())){
            vo.setPkTenant(RuntimeEnvironment.getTenantId());
        }
    }

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException{
        return approve(pk_bill, billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return reject(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        AlterVO alterVO = mapper.getById(pk_bill);
        return alterVO;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.ALTER_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<AlterVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(mapper);
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.ALTER_BILLTYPE));
        return deleteBpTemplate.delete((AlterVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
